package com.lf.lexy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.lf.comm.MyConstants;
import com.lf.lexy.business.BlogBusiness;
import com.lf.lexy.business.bac.FileBusiness;
import com.lf.lexy.dao.BlogContentDao;
import com.lf.lexy.dao.EnumsDao;
import com.lf.lexy.dao.GrabUploadInfDao;
import com.lf.lexy.dao.UserDao;
import com.lf.lexy.model.BlogContent;
import com.lf.lexy.model.Enums;
import com.lf.lexy.model.GrabUploadInf;
import com.lf.lexy.model.User;
import com.lf.lexy.model.dto.Page_BT;
import com.lf.lexy.webutils.CommMethod;
import com.lf.lexy.webutils.GetWaterId;

public class MainController extends BaseController {
	
	private BlogBusiness blogBusiness = new BlogBusiness();
	
	
	public int pageSize = MyConstants.FILE_MANAGER_PAGE_SIZE;
	
	
	public void toLoginPage(){
		render("login.jsp");

	}
	public void toLoginPageR(){
		redirect("/view/bac/login.jsp");

	}
	public void toPushBlog(){
		//render("new_bowen.jsp");
		//render("bac_blog_push.jsp");
		
		String upload_id = GetWaterId.getWaterId();
		setAttr("serialId", upload_id);
		render("bac_blog_push_20171020.jsp");

	}
	
	
	
	
	public void toBoWenList(){
		
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			//super.success("请先登录在操作！");
			toLoginPageR();
			return;
		}
//		Page<BlogContent> blogContentList = blogBusiness.getBlogContentList(user.getUserNo() ,  null, "1");
//		setAttr("bloglist", blogContentList);
//		List<Record> typelist = blogBusiness.getBlogTypeList(user.getUserNo());
//		setAttr("typelist", typelist);
		render("bac_blog_list.jsp");
	}
	
	
	
	
	/**
	 * 文件管理页面
	 * 
	 * 
	 * 
	 */

	
	
	public void toFileManager1(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		Page<GrabUploadInf> GrabUploadInfPage = GrabUploadInfDao.me.getGrabUploadInfPage(1, pageSize, null, null, null);
		setAttr("fileList", GrabUploadInfPage);
		render("file_manager_1.jsp");
		
		//super.success(GrabUploadInfPage);
	}
	
	
	
	
	/**
	 * 
	 */
	public void login(){
		
		String userName = getPara("userName");
		String userPwd = getPara("userPwd");
		User user = UserDao.me.getUser(userName, userPwd);
		
		if(user==null){
			setAttr("errMsg", "不存在该用户!");
			render("login.jsp");
			return;
		}
		
		if(user!=null){
			String isDelete = user.getIsDelete();
			if(MyConstants.IS_DELETE_Y.equals(isDelete)){
				setAttr("errMsg", "该用户被禁止登录,请联系管理员!");
				render("login.jsp");
				return;
			}else{
				setSessionAttr(MyConstants.SESSION_USER, user);
				render("index.jsp");
				return;
				
			}
		}
	}
	
	
	
	
	
	
	
	/**
	 * 
	 */
	public void saveAndUpdateBlogContent(){
		
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		String reqStr = getPara("reqStr");
		Map<String, Object> map = CommMethod.getMap();
		
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		Map parseObject = MyConstants.json.parseObject(reqStr, Map.class);
		if(parseObject!=null){
			
			String serialid = (String)parseObject.get("serialId");
			String id = "";
			String content = (String)parseObject.get("blogContent");
			System.out.println(content);
			content = content.replace("http://127.0.0.1:8080", "_ip_port");
			System.out.println(content);
			if(StringUtils.isEmpty(serialid)){
				id = blogBusiness.insertBlogCotent(parseObject , user);
			}else{
				id = blogBusiness.updateBlogCotent(parseObject, user);
			}
			map.put("id", id);
		}
		super.success(map);
		
	}
	
	
	/**
	 * 
	 */
	public void getBlogTypeCount(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		Map<String, Object> map = CommMethod.getMap();
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		List<Record> typelist = blogBusiness.getBlogTypeList(user.getUserNo());
		map.put("typelist", typelist);
		super.success(map);
	}
	
	
	
	public void insertBlogType(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		String par = super.getPar("reqStr");
		
		
		
		
	}
	@SuppressWarnings("static-access")
	public void getBlogContentPageInint(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		String reqStr = getPara("reqStr");
		
		@SuppressWarnings("unchecked")
		Map<String , String> parseObject = (Map<String , String>)MyConstants.json.parseObject(reqStr, Map.class);
		if(parseObject==null || parseObject.size()<1){
			super.success("请求参数不能为空！！");
			return;
		}
		
		String pageNum = parseObject.get("pageNum");
		String typeNo = parseObject.get("typeNo");
		
		Map<String, Object> map = CommMethod.getMap();
		if(StringUtils.isEmpty(pageNum)){
			super.success("请求参数不能为空！");
			return;
			
		}
		Page<BlogContent> blogContentList = blogBusiness.getBlogContentList(user.getUserNo() , typeNo , pageNum,MyConstants.PAGE_SIZE );
		map.put("bloglist", blogContentList);
		super.success(map);
		
	}
	
	
	public void getBlogContent(){
		
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		String reqStr = getPara("reqStr");
		
		@SuppressWarnings("unchecked")
		Map<String , String> parseObject = (Map<String , String>)MyConstants.json.parseObject(reqStr, Map.class);
		String serialId = parseObject.get("serialId");
		BlogContent geetBlogContentByPK = BlogContentDao.me.geetBlogContentByPK(serialId);
		success(geetBlogContentByPK);
	}
	
	
	
	
	
	/******************************************文件管理***************************************************/
	//TODO 文件管理
	
	  
	
	public void getFileTypeList(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		
		
		
	}
	
	
	  
	/**  
	* @Title: toFileManager  
	* @Description: 获取 文件列表
	* @param 
	* @return void
	* @author lxliufei
	* @date 上午10:59:10
	* @throws  
	*/  
	    
	public void toFileManager(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		
		String str = super.getPar("reqStr");
		HashMap<String, String> parseObject = MyConstants.json.parseObject(str , new HashMap<String, String>().getClass());
		String pageNumber = (String)parseObject.get("pageNum");
		String pageSizeStr = (String)parseObject.get("pageSizeStr");
		
		
		int _page_size = pageSize;
		if(StringUtils.isEmpty(pageSizeStr) || "undefined".equals(pageSizeStr)){
			_page_size = pageSize;
		}else{
			_page_size = Integer.parseInt(pageSizeStr);
		}
		Page<GrabUploadInf> GrabUploadInfPage = GrabUploadInfDao.me.getGrabUploadInfPage(Integer.parseInt(pageNumber), _page_size, null, null, null);
		super.success(GrabUploadInfPage);
	}
	/**  
	* @Title: fileUpload  
	* @Description: 附件上传  
	* @param 
	* @return void
	* @author lxliufei
	* @date 上午10:58:17
	* @throws  
	*/  
	    
	public void fileUpload(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		UploadFile file = getFile();
		
		Map<String, Object> map = CommMethod.getMap();
		if(file == null || file.getFile().length()<0){
			
		}
		String serialId = getPara("serialId");
		
		if(StringUtils.isEmpty(serialId)){
			serialId = GetWaterId.getWaterId();
		}
		
		
		String fileUpload = blogBusiness.fileUpload(file , serialId);
		
		map.put("path", fileUpload.replace("\\", "/"));
		super.success(map);
	}
	
	
	
	
	/******************编辑器上传附件*********/
	
	
	public void summernoteUpload(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		Map<String, Object> map = CommMethod.getMap();
		UploadFile file = getFile();
		String serialId = getPara("serialId");
		String fileUpload = FileBusiness.bus.fileUpload(file, serialId , user);
		
		
		String substring = fileUpload.substring(fileUpload.indexOf("upload"), fileUpload.length());
		
		String geetEnumsVal = EnumsDao.me.geetEnumsVal("_local_ip");
		
		fileUpload = "http://"+geetEnumsVal+ "/"+substring;
		map.put("imgUrl", fileUpload);
		super.success(map);
	}
	
	
	
	
	
	
	
	/**********************************************系统设置**************************************************/
	//TODO  系统设置 -  参数 配置
	  
	/**  
	* @Title: getEnumsList  
	* @Description: 获取参数列表
	* @param 
	* @return void
	* @author lxliufei
	* @date 上午10:56:08
	* @throws  
	*/  
	    
	public void getEnumsList(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		
		String order = getPara("order");
		String limit = getPara("limit");
		String offset = getPara("offset");
		int pageNum = 1;
		if(Integer.parseInt(offset)==0){
			pageNum = 1;
		}else{
			pageNum = Integer.parseInt(offset)/Integer.parseInt(limit) +1;
		}
		
//		get("limit")
		//Page<Enums> list = EnumsDao.me.geetParentEnumsList(pageNum, Integer.parseInt(limit), order);
		//map.put("", arg1)
		
		//super.success(geetParentEnumsList);
		//Page_BT<Enums> pageBT = new Page_BT<Enums>(list.getTotalRow(), list.getList());
		renderJson("");
		
	}
	
	
	/***********************************************************************************************************/
	
	
	
	
	
	
	
	
	
	
	

}
